package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.BuyPurchaseBillItem;
import com.ruoyi.buy.domain.entity.PO.BuyPurchaseInvItem;
import com.ruoyi.buy.domain.entity.PO.BuyPurchaseInvoice;
import com.ruoyi.buy.domain.entity.buy0003.Buy0003AddInvAndItem;
import com.ruoyi.buy.mapper.PO.BuyPurchaseBillItemMapper;
import com.ruoyi.buy.mapper.PO.BuyPurchaseInvItemMapper;
import com.ruoyi.buy.mapper.PO.BuyPurchaseInvoiceMapper;
import com.ruoyi.buy.service.IBuyPurchaseInvoiceService;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 发票Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-03
 */
@Service
public class BuyPurchaseInvoiceServiceImpl implements IBuyPurchaseInvoiceService
{
    @Resource
    private BuyPurchaseInvoiceMapper buyPurchaseInvoiceMapper;

    @Resource
    private ICommonService commonService;

    @Resource
    private BuyPurchaseInvItemMapper buyPurchaseInvItemMapper;

    @Resource
    private BuyPurchaseBillItemMapper buyPurchaseBillItemMapper;

    /**
     * 查询发票
     * 
     * @param id 发票主键
     * @return 发票
     */
    @Override
    public BuyPurchaseInvoice selectBuyPurchaseInvoiceById(Long id)
    {
        return buyPurchaseInvoiceMapper.selectBuyPurchaseInvoiceById(id);
    }

    @Override
    public Buy0003AddInvAndItem view(Long id) {
        BuyPurchaseInvoice buyPurchaseInvoice = this.buyPurchaseInvoiceMapper.selectBuyPurchaseInvoiceById(id);

        BuyPurchaseInvItem buyPurchaseInvItem = new BuyPurchaseInvItem();
        buyPurchaseInvItem.setnMainId(id);
        List<BuyPurchaseInvItem> list = this.buyPurchaseInvItemMapper.selectBuyPurchaseInvItemList(buyPurchaseInvItem);

        Buy0003AddInvAndItem result = new Buy0003AddInvAndItem();
        result.setBuyPurchaseInvoice(buyPurchaseInvoice);
        result.setBuyPurchaseInvItems(list);
        return result;
    }

    /**
     * 查询发票列表
     * 
     * @param buyPurchaseInvoice 发票
     * @return 发票
     */
    @Override
    public List<BuyPurchaseInvoice> selectBuyPurchaseInvoiceList(BuyPurchaseInvoice buyPurchaseInvoice)
    {
        return buyPurchaseInvoiceMapper.selectBuyPurchaseInvoiceList(buyPurchaseInvoice);
    }

    /**
     * 新增发票
     * 
     * @param para 发票
     * @return 结果
     */
    @Override
    public Buy0003AddInvAndItem insertBuyPurchaseInvoice(Buy0003AddInvAndItem para)
    {
        //新增发票
        BuyPurchaseInvoice buyPurchaseInvoice = para.getBuyPurchaseInvoice();
        buyPurchaseInvoice.setCreateTime(DateUtils.getNowDate());
        buyPurchaseInvoice.setCreateBy(commonService.getUsername());
        this.buyPurchaseInvoiceMapper.insertBuyPurchaseInvoice(buyPurchaseInvoice);
        //反查nMainId
        buyPurchaseInvoice = this.buyPurchaseInvoiceMapper.selectBuyPurchaseInvoiceList(buyPurchaseInvoice).get(0);
        Long nMainId = buyPurchaseInvoice.getId();

        //处理发票明细
        List<BuyPurchaseInvItem> buyPurchaseInvItems = para.getBuyPurchaseInvItems();
        if (buyPurchaseInvItems != null && !buyPurchaseInvItems.isEmpty()){

            for (BuyPurchaseInvItem item:buyPurchaseInvItems
            ) {
                //新增发票信息
                item.setnMainId(nMainId);
                item.setnQty(item.getnQty());
                item.setCreateBy(commonService.getUsername());
                item.setCreateTime(DateUtils.getNowDate());
                this.buyPurchaseInvItemMapper.insertBuyPurchaseInvItem(item);

                //处理合同表已开发票数量
                this.updatePurchaseInvQty(item);
            }

        }


        //查询数据回显
        Buy0003AddInvAndItem result = this.view(nMainId);
        return result;
    }

    /**
     * 修改发票
     * 
     * @param para 发票
     * @return 结果
     */
    @Override
    public Buy0003AddInvAndItem updateBuyPurchaseInvoice(Buy0003AddInvAndItem para)
    {
        //修改发票
        BuyPurchaseInvoice buyPurchaseInvoice = para.getBuyPurchaseInvoice();
        buyPurchaseInvoice.setUpdateTime(DateUtils.getNowDate());
        buyPurchaseInvoice.setUpdateBy(commonService.getUsername());
        this.buyPurchaseInvoiceMapper.updateBuyPurchaseInvoice(buyPurchaseInvoice);

        Long nMainId = buyPurchaseInvoice.getId();

        //处理发票明细
        List<BuyPurchaseInvItem> buyPurchaseInvItems = para.getBuyPurchaseInvItems();
        for (BuyPurchaseInvItem item:buyPurchaseInvItems
        ) {
            if (item.getId() == null || item.getId() == 0){
                //新增发票信息
                item.setnMainId(nMainId);
                item.setnQty(item.getnQty());
                item.setCreateBy(commonService.getUsername());
                item.setCreateTime(DateUtils.getNowDate());
                this.buyPurchaseInvItemMapper.insertBuyPurchaseInvItem(item);

                //处理合同表已开发票数量
                this.updatePurchaseInvQty(item);
            }else{
                if (item.getnQty() != item.getnOldQty() || item.getnOldPrice() != item.getnPrice() ){
                    item.setUpdateBy(commonService.getUsername());
                    item.setUpdateTime(DateUtils.getNowDate());
                    this.buyPurchaseInvItemMapper.updateBuyPurchaseInvItem(item);
                    //处理合同表已开发票数量
                    this.updatePurchaseInvQty(item);
                }
            }

        }

        //查询数据回显
        Buy0003AddInvAndItem result = this.view(nMainId);
        return result;
    }

    /**
     * 批量删除发票
     * 
     * @param ids 需要删除的发票主键
     * @return 结果
     */
    @Override
    public int deleteBuyPurchaseInvoiceByIds(Long[] ids)
    {
        return buyPurchaseInvoiceMapper.deleteBuyPurchaseInvoiceByIds(ids);
    }

    /**
     * 删除发票信息
     * 
     * @param id 发票主键
     * @return 结果
     */
    @Override
    public int deleteBuyPurchaseInvoiceById(Long id)
    {
        return buyPurchaseInvoiceMapper.deleteBuyPurchaseInvoiceById(id);
    }

    @Override
    public List<BuyPurchaseInvItem> getNotInvItem(BuyPurchaseInvItem para) {
        return this.buyPurchaseInvoiceMapper.getNotInvItem(para);
    }

    @Override
    public int delItem(BuyPurchaseInvItem para) {
        this.buyPurchaseInvItemMapper.deleteBuyPurchaseInvItemById(String.valueOf(para.getId()));
        this.updatePurchaseInvQty(para);
        return 1;
    }

    private void updatePurchaseInvQty(BuyPurchaseInvItem item){
        //先找到对应的合同件
        BuyPurchaseBillItem buyPurchaseBillItem = this.buyPurchaseInvoiceMapper.getPurchaseBillItem(item);

        //再查已开发票数量
        BigDecimal nInvQty = this.buyPurchaseInvoiceMapper.getInvQty(item);
        buyPurchaseBillItem.setnInvQty(nInvQty);
        buyPurchaseBillItem.setUpdateBy(commonService.getUsername());
        buyPurchaseBillItem.setUpdateTime(DateUtils.getNowDate());
        this.buyPurchaseBillItemMapper.updateBuyPurchaseBillItem(buyPurchaseBillItem);
    }
}
